//
//  ViewController.m
//  Test
//
//  Created by 祝文博 on 15/3/3.
//  Copyright (c) 2015年 KevinLab. All rights reserved.
//

#import "MainViewController.h"
#import "Tab1ViewController.h"
#import "Tab2ViewController.h"
#import "Tab3ViewController.h"
#import "Tab4ViewController.h"
#import "MyTableViewController.h"
#import "TabBarItem.h"

@interface MainViewController ()
@property (nonatomic, strong) UIView *tabBarView;
@property (nonatomic, strong) TabBarItem *selectedItem;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.hidden = YES;
    // Do any additional setup after loading the view, typically from a nib.
//    self.tabBar.translucent = NO;
//    self.navigationController.tabBarController.tabBar.translucent = NO;
//    NSLog(@"tabbar frame : %@", NSStringFromCGRect(self.tabBar.frame));
    
    Tab1ViewController *tab1ViewController = [[Tab1ViewController alloc] init];
//    Tab2ViewController *tab2ViewController = [[Tab2ViewController alloc] init];
    Tab3ViewController *tab3ViewController = [[Tab3ViewController alloc] init];
    Tab4ViewController *tab4ViewController = [[Tab4ViewController alloc] init];
    MyTableViewController *myTableViewController = [[MyTableViewController alloc] init];
//    myTableViewController.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.viewControllers = @[tab1ViewController, myTableViewController, tab3ViewController, tab4ViewController];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"淘汽点评";
    //    NSLog(@"tabbar frame : %@", NSStringFromCGRect(self.tabBar.frame));
    self.tabBarView = [[UIView alloc] initWithFrame:self.tabBar.frame];
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.frame.size.width, 0.5)];
    //    lineLabel.backgroundColor = [UIColor colorWithHex:0xff0000];
    lineLabel.backgroundColor = [UIColor blackColor];
    [self.tabBarView addSubview:lineLabel];
    [self.view addSubview:self.tabBarView];
    //    NSLog(@"tabbar frame : %@", NSStringFromCGRect(self.tabBar.frame));
    
    NSArray *titles = @[@"Tab1", @"Tab2", @"Tab3", @"Tab4"];
    int numOfTab = 4;
    for (int i = 0; i < numOfTab; i++) {
        NSString *normal, *selected;
        
        TabBarItem *tabBarItem = [[TabBarItem alloc] initWithFrame:CGRectMake(i * (self.view.frame.size.width / numOfTab), 0, self.view.frame.size.width / numOfTab, _tabBarView.frame.size.height)];
        [tabBarItem setBackgroundColor:[UIColor colorWithHex:0xB068FB]];
        normal = [NSString stringWithFormat:@"tab_icon_%d", i+1];
        selected = [NSString stringWithFormat:@"tab_icon_%d_selected", i+1];
        
        [tabBarItem setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
        [tabBarItem setImage:[UIImage imageNamed:selected] forState:UIControlStateSelected];
        
        [tabBarItem setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
        [tabBarItem setTitle:[titles objectAtIndex:i] forState:UIControlStateSelected];
        [tabBarItem addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchDown];
        tabBarItem.tag = i;
        [self.tabBarView addSubview:tabBarItem];
        
        if (i == 0) {
            _selectedItem = tabBarItem;
            tabBarItem.selected = YES;
            [self itemClick:tabBarItem];
        }
    }

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)itemClick:(TabBarItem *)item
{
//    NSLog(@"%ld", (long)item.tag);
    if (_selectedItem.tag != item.tag) {
        self.selectedIndex = item.tag;
        item.selected = YES;
        _selectedItem.selected = NO;
        _selectedItem = item;
    }
}



@end
